package cn.stylefeng.guns.modular.rm.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.rm.entity.FbGame;
import cn.stylefeng.guns.modular.rm.entity.RmGame;
import cn.stylefeng.guns.modular.rm.mapper.FbGameMapper;
import cn.stylefeng.guns.modular.rm.mapper.RmGameMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 16:45
 **/
@Service
public class FbGameService extends ServiceImpl<FbGameMapper, FbGame> {
    @Autowired
    private FbGameMapper fbGameMapper;

    public List<String> getGameids() {
        return getGameids(DateUtil.beginOfDay(new Date()));
    }
    public List<String> getGameids(Date date) {
        LambdaQueryWrapper<FbGame> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.ge(FbGame::getDate, date);

        List<FbGame> fbGames = fbGameMapper.selectList(lambdaQuery);
        return fbGames.stream().map(FbGame::getGameid).collect(Collectors.toList());
    }

    public String getGame(JSONObject jsonObject) {
        List<String> ids = getGameids();

        List<FbGame> list = new ArrayList<>();

        JSONArray recodes = jsonObject.getJSONObject("data").getJSONArray("records");
        for (int i = 0; i < recodes.size(); i++) {
            JSONObject recode = recodes.getJSONObject(i);
            JSONArray ops = recode.getJSONArray("mg")
                    .getJSONObject(0)
                    .getJSONArray("mks")
                    .getJSONObject(0)
                    .getJSONArray("op");
            String teamA = ops.getJSONObject(0).getString("na");
            String teamB = ops.getJSONObject(1).getString("na");

            String league = recode.getJSONObject("lg").getString("na");
            String id = recode.getString("id");

            if (!ids.contains(id)) {
                FbGame fbGame = FbGame.builder()
                        .gameid(id)
                        .league(league)
                        .teamA(teamA)
                        .teamB(teamB)
                        .date(new Date(recode.getLong("bt")))
                        .build();
                list.add(fbGame);
            }
        }
        this.saveBatch(list);
        return null;
    }

    public JSONObject getGames(String type, String gameid, Integer current) {
        JSONObject params = new JSONObject();
        params.put("type", type);
        String url = null;
        switch (type) {
            case "0":
                url = StrUtil.format("http://124.222.75.100:8089/cp/fbmarket?type=0&current={}", current);
                break;
            case "1":
                url = StrUtil.format("http://124.222.75.100:8089/cp/fbmarket?type=1&gameid={}", gameid);
                break;
        }

        String body = HttpUtil.createGet(url)
                .execute().body();
        return JSONObject.parseObject(body);
    }
}
